package com.lyt.springboot.util.algorithm;

public class S4 {
    //快速排序
    public void quickSort(int[] arr ,int left,int right) {
        //选择一个基准key值中间轴
        int key = arr[left];
        int i =left;
        int j = right;

        //保证i<j
        while(i<j) {
            //先移动右指针
            while(i<j && arr[j]>= key) {
                j--;
            }
            //再移动左指针
            while(i<j && arr[i]<key) {
                i++;
            }
            //交换左右指针的值
            if(i<j) {
                swap(arr,i,j);
            }
        }
        //循环之后交换值
        swap(arr,i,left);
        //递归
        quickSort(arr,left,i-1);
        quickSort(arr,j+1,right);

    }
    private int[] swap(int[]arr,int i,int j ) {
        int a  = arr.length -1;
        if(i<=a && j<=a) {
            int temp  = arr[i];
            arr[i]  = arr[j];
            arr[j] = temp;
        }
        return arr;
    }

    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        S4 s4 = new S4();
        int[] swap = s4.swap(arr1, 3, 1);
        for (int i = 0; i <swap.length ; i++) {
            System.out.println(swap[i]);
        }
    }
}
